package cn.mitool.core.util;

import cn.mitool.core.constant.HttpStatus;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * 统一响应信息主体
 *
 * @author dragon
 * @version 1.0
 * @date 2024/6/28 10:24
 */
@Getter
@Setter
public class AjaxResult<T> implements Serializable {

    /** 成功 */
    public static final int SUCCESS = HttpStatus.SUCCESS;

    /** 失败 */
    public static final int FAIL = HttpStatus.ERROR;

    /**
     * 响应状态 true:成功 false:失败:
     */
    private Boolean isSuccessful;

    /**
     * 请求时间
     */
    private LocalDateTime timestamp;

    /**
     * 追踪ID
     */
    private String traceId;

    /**
     * 响应状态码
     */
    private Integer code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;


    public static <T> AjaxResult<T> succeed() {
        AjaxResult<T> ajaxResult = new AjaxResult<>();

        ajaxResult.setIsSuccessful(Boolean.TRUE);
        ajaxResult.setTimestamp(LocalDateTime.now());
        ajaxResult.setTraceId(UUID.randomUUID().toString());
        ajaxResult.setCode(SUCCESS);
        ajaxResult.setMessage("操作成功");

        return ajaxResult;
    }

    public static <T> AjaxResult<T> failed() {
        AjaxResult<T> ajaxResult = new AjaxResult<>();

        ajaxResult.setIsSuccessful(Boolean.FALSE);
        ajaxResult.setTimestamp(LocalDateTime.now());
        ajaxResult.setTraceId(UUID.randomUUID().toString());
        ajaxResult.setCode(FAIL);
        ajaxResult.setMessage("操作失败");

        return ajaxResult;
    }

    public static <T> AjaxResult<T> succeed(String message) {
        AjaxResult<T> ajaxResult = succeed();
        ajaxResult.setMessage(message);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> succeed(String message, int code) {
        AjaxResult<T> ajaxResult = succeed();
        ajaxResult.setMessage(message);
        ajaxResult.setCode(code);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> succeed(int code) {
        AjaxResult<T> ajaxResult = succeed();
        ajaxResult.setCode(code);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> succeed(int code, String message) {
        AjaxResult<T> ajaxResult = succeed();
        ajaxResult.setCode(code);
        ajaxResult.setMessage(message);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> succeedWith(T data) {
        AjaxResult<T> ajaxResult = succeed();
        ajaxResult.setData(data);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> succeedWith(T data, int code) {
        AjaxResult<T> ajaxResult = succeedWith(data);
        ajaxResult.setCode(code);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> succeedWith(T data, int code, String message) {
        AjaxResult<T> ajaxResult = succeedWith(data);
        ajaxResult.setCode(code);
        ajaxResult.setMessage(message);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> succeedWith(T data, String message) {
        AjaxResult<T> ajaxResult = succeedWith(data);
        ajaxResult.setMessage(message);
        ajaxResult.setData(data);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> succeedWith(T data, String message, int code) {
        AjaxResult<T> ajaxResult = succeedWith(data);
        ajaxResult.setCode(code);
        ajaxResult.setMessage(message);
        ajaxResult.setData(data);
        return ajaxResult;
    }


    public static <T> AjaxResult<T> failed(String message) {
        AjaxResult<T> ajaxResult = failed();
        ajaxResult.setMessage(message);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> failed(String message, T data) {
        AjaxResult<T> ajaxResult = failed();
        ajaxResult.setMessage(message);
        ajaxResult.setData(data);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> failed(String message, int code) {
        AjaxResult<T> ajaxResult = failed();
        ajaxResult.setCode(code);
        ajaxResult.setMessage(message);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> failed(String message, int code, T data) {
        AjaxResult<T> ajaxResult = failed();
        ajaxResult.setCode(code);
        ajaxResult.setMessage(message);
        ajaxResult.setData(data);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> failed(String message, T data, int code) {
        AjaxResult<T> ajaxResult = failed();
        ajaxResult.setCode(code);
        ajaxResult.setMessage(message);
        ajaxResult.setData(data);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> failed(int code) {
        AjaxResult<T> ajaxResult = failed();
        ajaxResult.setCode(code);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> failed(int code, String message) {
        return failed(message, code);
    }

    public static <T> AjaxResult<T> failed(int code, String message, T data) {
        AjaxResult<T> ajaxResult = failed();
        ajaxResult.setCode(code);
        ajaxResult.setMessage(message);
        ajaxResult.setData(data);
        return ajaxResult;
    }

    @Override
    public String toString() {
        return "AjaxResult{" +
            "isSuccessful=" + isSuccessful +
            ", timestamp=" + timestamp +
            ", traceId='" + traceId + '\'' +
            ", code=" + code +
            ", message='" + message + '\'' +
            ", data=" + data +
            '}';
    }
}
